﻿using ConsoleApp.TestFunc.Models;
using Microsoft.EntityFrameworkCore;

namespace ConsoleApp.TestFunc.ConnDB
{
    public class BloggingContext : DbContext
    {
        public DbSet<Blog> Blogs { get; set; }
        public DbSet<Post> Posts { get; set; }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            // 替换为你的数据库连接字符串
            /*optionsBuilder.UseMySql(
                "Server=your_server;Database=your_db;User ID=your_user;Password=your_password;",
                ServerVersion.AutoDetect("Server=your_server;Database=your_db;User ID=your_user;Password=your_password;"));
            */
            optionsBuilder.UseMySQL(@"server=localhost;Database=it_test;Uid=root;Pwd=123456");

        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);

            // 可选：显式配置外键关系（可省略，因为 EF Core 已通过 [ForeignKey] 自动识别）
            modelBuilder.Entity<Post>()
                .HasOne(p => p.Blog)
                .WithMany(b => b.Posts)
                .HasForeignKey(p => p.BlogId);
        }
    }
}
